package com.video.tiktokdownloader.nowatermark

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import com.google.ads.mediation.facebook.FacebookAdapter
import com.google.ads.mediation.facebook.FacebookExtras
import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.InterstitialAd
import com.google.android.gms.ads.MobileAds
import kotlinx.android.synthetic.main.activity_home.*
import kotlinx.android.synthetic.main.activity_home.home_setting
import kotlinx.android.synthetic.main.fragment_home.*


class HomeActivity : BaseActivity() {
    companion object {
        fun getAdRequest(): AdRequest {
            var extras = FacebookExtras()
                .setNativeBanner(true)
                .build()
            return AdRequest.Builder()
                .addNetworkExtrasBundle(FacebookAdapter::class.java, extras)
                .build()
        }
    }

    private var mInterstitialAd: InterstitialAd? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home)

        home_setting.setOnClickListener {

            startActivity(Intent(this, SettingActivity::class.java))
        }

        home_menu_home.setOnClickListener {
            home_fragment_home_fl.visibility = View.VISIBLE
            home_fragment_saved_fl.visibility = View.GONE

            home_menu_home.setBackgroundResource(R.drawable.home_bottom_menu_select_bg)
            home_menu_home_icon.setImageResource(R.drawable.home_white)
            home_menu_home_name.setTextColor(resources.getColor(R.color.home_menu_text_select_color))

            home_menu_download.setBackgroundResource(R.drawable.home_bottom_menu_bg)
            home_menu_download_icon.setImageResource(R.drawable.download_black)
            home_menu_download_name.setTextColor(resources.getColor(R.color.home_menu_text_color))
        }

        home_menu_download.setOnClickListener {
            home_fragment_home_fl.visibility = View.GONE
            home_fragment_saved_fl.visibility = View.VISIBLE

            home_menu_home.setBackgroundResource(R.drawable.home_bottom_menu_bg)
            home_menu_home_icon.setImageResource(R.drawable.shouye)
            home_menu_home_name.setTextColor(resources.getColor(R.color.home_menu_text_color))

            home_menu_download.setBackgroundResource(R.drawable.home_bottom_menu_select_bg)
            home_menu_download_icon.setImageResource(R.drawable.download_white)
            home_menu_download_name.setTextColor(resources.getColor(R.color.home_menu_text_select_color))

        }

//        initAd()
    }

    private fun initAd() {
        MobileAds.initialize(
            this
        ) {
        }
        adView.loadAd(getAdRequest())

        mInterstitialAd = InterstitialAd(this)
        mInterstitialAd?.adUnitId = getString(R.string.admob_interstitial_id)
        mInterstitialAd?.loadAd(
            getAdRequest()
        )
        mInterstitialAd?.adListener = object : AdListener() {
            override fun onAdClosed() {
                mInterstitialAd?.loadAd(AdRequest.Builder().build())
            }
        }
    }

    override fun showInterstitial(any: Any) {
        super.showInterstitial(any)
        if (mInterstitialAd?.isLoaded == true) {
            mInterstitialAd?.show();
        } else {
            Log.d("TAG", "The interstitial wasn't loaded yet.");
        }
    }
}
